package com.cloudcc.mobile.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.cloudcc.cloudframe.adapter.CommonAdapter;
import com.cloudcc.cloudframe.adapter.ViewHolder;

public abstract class BaseChoiceAdapter<T> extends CommonAdapter<T> {
	protected List<T> mChoiceList;

	public BaseChoiceAdapter(Context mContext) {
		super(mContext);
		mChoiceList = new ArrayList<T>();
	}

	/**
	 * 获得选中的
	 * 
	 * @return
	 */
	public List<T> getChoices() {
		return mChoiceList;
	}

	/**
	 * 判断是否选中
	 * 
	 * @param t
	 * @return
	 */
	protected boolean hasContained(T t) {
		for (T item : mChoiceList) {
			if (isEquals(t, item)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 添加到选中数组
	 * 
	 * @param t
	 */
	protected void addToChoice(T t) {
		mChoiceList.add(t);
	}

	public void changeChoices(List<T> array) {
		mChoiceList.clear();
		mChoiceList.addAll(array);
	}

	/**
	 * 从选中的数组中删除一个item
	 * 
	 * @param t
	 */
	protected void removeFromChoice(T t) {
		int removeIndex = -1;
		for (int i = 0; i < mChoiceList.size(); i++) {
			T t1 = mChoiceList.get(i);
			if (isEquals(t, t1)) {
				removeIndex = i;
				break;
			}
		}
		try {
			if (removeIndex >= 0) {
				mChoiceList.remove(t);
			}
		} catch (Exception e) {
		}
	}

	/**
	 * 判断两个 对象相同 用户 点击 删除的时候
	 * 
	 * @param t1
	 * @param t2
	 * @return
	 */
	protected abstract boolean isEquals(T t1, T t2);
}
